package com.treasure.service.collocate.controller;

import com.treasure.service.annotation.LogInfo;
import com.treasure.service.collocate.service.BankConfigService;
import com.treasure.service.commons.lang.util.Base64Util;
import com.treasure.service.commons.lang.util.BeanUtil;
import com.treasure.service.commons.lang.util.IdCreater;
import com.treasure.service.domain.TBankConfig;
import com.treasure.service.dto.TBankConfigDto;
import com.treasure.service.dto.TCreditCardDto;
import com.treasure.service.result.PageResult;
import com.treasure.service.result.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 获取银行相关配置信息
 * @version 1.0
 * @author panxinwen 2018/10/30 10:32
 * @Copyright © 2015 | 武汉京航科技有限公司 Email:19919866605@163.com
 * */
@Api(value = "银行或渠道商配置信息",description = "银行或渠道商配置信息")
@RestController
@RequestMapping("/bankConfig/v1")
public class BankConfigController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BankConfigService bankConfigService;

    @Value("${treasure.file.path}")
    private String IMG_PATH;

    @ApiOperation(value = "获取银行或渠道配置列表", notes = "获取银行或渠道配置列表")
    @PostMapping("/getBankConfigList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "頁碼", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每頁顯示數量", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "当前状态(0-禁用 1-启用)", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "recommend", value = "是否首页显示", required = true, dataType = "int", paramType = "query")
    })
    @LogInfo(log = "获取银行或渠道配置列表",isPrint = true)
    public ResponseEntity getBankConfigList(@RequestParam(value = "pageSize") Integer pageSize,
                                            @RequestParam(value = "pageNum") Integer pageNum,
                                            @RequestParam(value = "status",required = false) Integer status,
                                            @RequestParam(value = "recommend",required = false) Integer recommend
    ){

        PageResult<List<TBankConfigDto>> result = bankConfigService.getBankConfigList(pageSize,pageNum,status,recommend);

        return ResponseEntity.ok(result);

    }

    @ApiOperation(value = "新建或更新银行配置信息", notes = "新建或更新银行配置信息")
    @PostMapping("/updateBankConfig")
    @LogInfo(log = "获取银行或渠道配置列表",isPrint = true)
    public ResponseEntity updateBankConfig(@RequestBody TBankConfigDto tBankConfig){

        String img =tBankConfig.getImg().replace("data:image/jpeg;base64,", "");
        String imgName = IdCreater.getLongId()+".jpg";
        imgName = Base64Util.generateImage(img, IMG_PATH+imgName);
        tBankConfig.setBankUrl(imgName);

        bankConfigService.updateBankConfig(BeanUtil.copy(tBankConfig,TBankConfig::new));

        return ResponseEntity.ok(new ResponseResult());

    }


    @ApiOperation(value = "获取推荐列表(信用卡)", notes = "获取热门信用卡列表")
    @PostMapping("/getCreditList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "頁碼", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每頁顯示數量", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "渠道状态（0-禁用 1-启用）", required = true, dataType = "int", paramType = "query")
    })
    @LogInfo(log = "获取银行或渠道配置列表",isPrint = true)
    public ResponseEntity getCreditList(@RequestParam(value = "pageSize") Integer pageSize,
                                        @RequestParam(value = "pageNum") Integer pageNum,
                                        @RequestParam(value = "status",required = false,defaultValue = "1") Integer status
    ){

        PageResult<List<TCreditCardDto>> result = bankConfigService.getCreditList(pageSize,pageNum,status);

        return ResponseEntity.ok(result);

    }
}
